﻿// JavaScript Document

var map;
var vietnam = new google.maps.LatLng(14.058324, 108.277199);
var us = new google.maps.LatLng(15.03, 104.23);
var initLocation;
var browserSupportFlag;
var geocoder;
var geocoderA;
var marker;
var infowindow;
var latitude;
var longitude;

var UserLat;
var UserLng;
var UserAdd;


//ham khoi tao
function initialize() {
    var myOptions = {
        zoom: 3,
		center: new google.maps.LatLng(0, 108.277199),
        mapTypeId: google.maps.MapTypeId.ROADMAP
		
    };

    map = new google.maps.Map(document.getElementById("map"), myOptions);

    
	var here = new google.maps.LatLng(0, 118.277199);
	marker = new google.maps.Marker({position: here, map:map});
	marker.setDraggable(true);
	
	geocoderA = new google.maps.Geocoder();
	var posH = marker.getPosition();
	geocoderA.geometry.location.lat = posH.lat;
	geocoderA.geometry.location.lng = posH.lgn;
	
	
	
	
	//map.setCenter(new google.maps.LatLng(0,0));
	//map.setZoom(2);
	
	  // Try W3C Geolocation (Preferred)
    if (navigator.geolocation) {
        browserSupportFlag = true;
        navigator.geolocation.getCurrentPosition(function (position) {
            initLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            map.setCenter(initLocation);
			//adding a marker
			var here = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
			var marker1 = new google.maps.Marker({position:here, map:map});
			
			//create a infoWindow
			var infoWindow = new google.maps.InfoWindow({content: 'My location'});
			infoWindow.open(map, marker);
			map.setZoom(20);
            latitude = position.coords.latitude;
            longitude = position.coords.longitude;
        }, function () {
            handleNoGeolocation(browserSupportFlag);
        });
    }

    // Try Google Gears Geolocation
    else if (google.gears) {
        browserSupportFlag = true;
        var geo = google.gears.factory.create('beta.geolocation');
        geo.getCurrentPosition(function (position) {
            initLocation = new google.maps.LatLng(position.latitude, position.longitude);
            map.setCenter(initLocation);
			
						//adding a marker
			var here = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
			var marker2 = new google.maps.Marker({position:here, map:map});
			
			//create a infoWindow
			var infoWindow = new google.maps.InfoWindow({content:'My location'});
			infoWindow.open(map, marker);
			map.setZoom(20);
            latitude = position.coords.latitude;
            longitude = position.coords.longitude;
        }, function () {
            handleNoGeolocation(browserSupportFlag);
        });
    }
	
	
		


    // Browser doesn't support Geolocation
    else {
        browserSupportFlag = false;
        handleNoGeolocation(browserSupportFlag);
    }
}

//ham xu ly khong dinh vi duoc
function handleNoGeolocation(errorFlag) {
    if (errorFlag == true) {
        alert("Dich vu dinh vi dia ly co loi!");
        initLocation = vietnam;
    }
    else {
        alert("Trinh duyet khong ho tro dinh vi dia ly!");
        initLocation = vietnam;
    }
    map.setCenter(initLocation);
}
//Tim dia diem
function findLocation(address, flag) {
    if (!geocoder) {
        geocoder = new google.maps.Geocoder();
    }
    var geocoderRequest = { address: address };
    geocoder.geocode(geocoderRequest, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK)
		{
			if (flag == true) 
			{
                var panel = document.getElementById("diadiempanel");
                var panelContent = "<strong>Cac ket qua tim duoc:</strong></br>";
                for (var i in results)
				{
                    panelContent += "<a href='javascript:void(0);' name='" + results[i].formatted_address + "' onclick=linkDiaDiem_Click(this)>" + results[i].formatted_address + "</a>" + "</br></br>";
                }
                panel.innerHTML = panelContent;
            }
            if (!marker) {
                marker = new google.maps.Marker({ map: map });
            }
			//marker.setDraggable(true);
			
            marker.setPosition(results[0].geometry.location);
			
			var posH = marker.getPosition();
			
            if (!infowindow) {
                infowindow = new google.maps.InfoWindow();
            }
			

		//	results[0].geometry.location = posH;
			var content = '<strong>' + results[0].formatted_address + '</strong></br>';
			UserAdd = results[0].formatted_address;
			
            content += 'Vi do:' + results[0].geometry.location.lat() + '</br>';
            UserLat = results[0].geometry.location.lat();
            
            content += 'Kinh do:' + results[0].geometry.location.lng() + '</br></br></br>';	
            UserLng = results[0].geometry.location.lng();
            		
			infowindow.setContent(content);
            infowindow.open(map, marker);
            google.maps.event.addListener(marker, 'click', function () {
                infowindow.open(map, marker);
            });
        }
        else {
            alert('Khong tim thay dia chi can tim');
        }
    });
}

//ham xu ly su kien button click
function btnDiaDiem_Click() {
    var address = document.getElementById("DiaDiem").value;
    findLocation(address, true);
}

//ham xu ly su kien link click
function linkDiaDiem_Click(link) {
    var DiaDiem = link.name;
    
    findLocation(DiaDiem, false);
}

//ham xu ly su kien button click
function btnMyLocation_Click() {

    map.setZoom(20);
    var content = 'Vi tri cua ban</br>' + 'Vi do: ' + latitude + '</br> kinh do: ' + longitude;
    var marker = new google.maps.Marker({
        position: us,
        map: map
    });
    // Creating a InfoWindow
    var infoWindow = new google.maps.InfoWindow({
        content: content
    });
    // Adding the InfoWindow to the map
    infoWindow.open(map, marker);

}

//se viet 1 ham truyen du lieu ma nguoi dung luu lai duoi dang ten va toa do cua vi tri do

function btnLuuViTri_Click() {
    //lay toa do và địa chỉ của marker đang đứng để lưu vào

    var receivevalue = document.getElementById('HiddenField1').value;
    
    var panel = document.getElementById("DiaDiem");
    var panelContent = receivevalue;
                
    panel.innerHTML = panelContent;
    
                
                
    var result = [UserLat, UserLng, UserAdd];    
    PageMethods.btnLuuViTri_Click(result, OnSuccessLuuViTri);
}
function OnSuccessLuuViTri(response) {
    for (var i in response) {
        alert(response[i]);
    }
}

function btnTest_Click() {
    //lay toa do và địa chỉ của marker đang đứng để lưu vào

    var receivevalue = document.getElementById('HiddenField1').value;
    
    alert(receivevalue);
                
   // panel.innerHTML = panelContent;
}

// function GetCityNameArray() {
//        var cities = ['Mumbai', 'Delhi', 'Chennai'];
//        PageMethods.GetCityNameArray(cities, OnSuccessGetCityNameArray);
//    }
//    function OnSuccessGetCityNameArray(response) {       
//        for (var i in response) {
//            alert(response[i]);
//        }
//    }



